(function($) {
	$.fn.Ray_MatchPicker = function(options) {
		var target = $(this);
		var params = $.extend({
			width: 300,				//width of the area
			maxResult: 10,			//max-height of the area
			hasAll: null,
			defaultVal: {id:99, name:"全部"},
			zIndex: 99999,	
			header: "",
			matchUrl: "",				//the request url of match result, only when isDynamic=true
			isSingle: true,				//single or multiple about selection
			isDynamic: true,			//whether the result is dynamic get. if false, isSingle auto true and maxResult Invalid
			resultData: {},	//the default value list, only when isDynamic=false
			resultFun: null,			//the function of the results, only when isDynamic=true
			changeFun: null,		//the function of the value changed
			confirmFun: null,		//the function of the confirm button clicked, only when isSingle=false
			itemClickFun: null				//if only for select items
		}, options);
		if(undefined != target.data('Ray_MatchPicker')){
			var _plugin = $(this).data('Ray_MatchPicker');
			_plugin.matchpicker.remove();
			$(document).unbind('mousedown', _plugin._mousedown);
			$(this).removeData('Ray_MatchPicker');
		} 
		// create a new instance of the plugin
		var plugin = new $.Ray_MatchPicker(target, params);
		// save a reference to the newly created object
		target.data('Ray_MatchPicker', plugin);
	};
	$.Ray_MatchPicker = function(target, params) {
		var plugin = this;
		target.attr('readonly', 'readonly');
		target.data("value", params.defaultVal.id).val(params.defaultVal.name);
		params.containDOM = "<div class=\"Ray_MatchPicker\" style=\"width:"+params.width+"px; z-index: "+params.zIndex+"\" >"+
		"<div class=\"mp_header\">"+
		"<span style=\"float:right;\">"+
		"<a class=\"clear\">清除</a>"+
		"<a class=\"confirm\">确定</a></span>"+
		"<span style=\"float:left\">"+
		"<input type=\"text\" /><a class=\"search\">检索</a></span></div>"+
		"<ul class=\"mp_content\" ></ul></div>";
		plugin.matchpicker = $(params.containDOM);
		$('body').append(plugin.matchpicker);
		if($.browser.msie) {
			plugin.matchpicker.addClass("ie");
		}
		params.matchDom = plugin.matchpicker.find("input").eq(0);
		params.matchVal = "";
		params.contentDom = plugin.matchpicker.find("ul").eq(0);
		if(!params.isDynamic) {
			params.isSingle = true;
			plugin.matchpicker.find(".mp_header").hide();
			var html = "";
			if(params.hasAll!=null) {
				html+='<li><a id="mp_'+params.hasAll.id+'">'+params.hasAll.name+'</a></li>';
			}
			for(var key in params.resultData) {
				html+='<li><a id="mp_'+key+'">'+params.resultData[key]+'</a></li>';
			}
			if(html=="") {
				html='<li style="margin-left:0px;">暂无数据</li>';
			}
			params.contentDom.html(html);
		}
		if(params.isSingle) {
			plugin.matchpicker.find(".confirm").hide();
		}
		if(params.header!="") {
			plugin.matchpicker.find("span").last().html(params.header);
		}
		if($.isFunction(params.itemClickFun)) {
			plugin.matchpicker.find(".confirm").show();
			plugin.matchpicker.find(".mp_header").show();
		} 
		target.bind("click", function(e){
			e.preventDefault();
			if (plugin.matchpicker.css('display') != 'none') {
				plugin.hide();
			} else {
				plugin.show();
				if(params.isDynamic && params.matchDom.val().isEmpty()) {
					params.resultFun(params.matchUrl, "", params.maxResult, plugin.format);
				}
			}
		});
		plugin.matchpicker.find(".clear").bind("click", function(e) {
			e.preventDefault();
			target.val("");
			target.data("value", params.defaultVal.id).val(params.defaultVal.name);
			plugin.hide();
		});
		plugin.matchpicker.find(".confirm").bind("click", function(e) {
			e.preventDefault();
			if($.isFunction(params.itemClickFun)) {
				plugin.hide();
				return;
			} 
			var preVal = target.val();
			var shows = new Array();
			var vals = new Array();
			params.contentDom.find(".mp_on").each(function(){
				var dom = $(this);
				shows.push(dom.html());
				vals.push(dom.attr("id").substr(3));
			});
			target.val(shows.join(","));
			target.data("value", vals.join(","));
			if($.isFunction(params.confirmFun)) {
				params.confirmFun(shows, vals);
			}
			if($.isFunction(params.changeFun) && preVal!=target.val()) {
				params.changeFun(preVal, target.val());
			}
			target.keyup();
			plugin.hide();
		});
		plugin.matchpicker.find(".search").bind("click", function(e) {
			e.preventDefault();
			if(!$.isFunction(params.resultFun)) {
				return;
			}
			var match = $.trim(params.matchDom.val());
			params.contentDom.html('<li class="waiting"></li>');
			params.resultFun(params.matchUrl, match, params.maxResult, plugin.format);
		});
		params.matchDom.bind("keyup", function(e) {
			e.preventDefault();
			if(!$.isFunction(params.resultFun)) {
				return;
			}
			if(e.keyCode == 13) {
				var match = $.trim(params.matchDom.val());
				params.contentDom.html('<li class="waiting"></li>');
				params.resultFun(params.matchUrl, match, params.maxResult, plugin.format);
			}
			// if(match==params.matchVal) {
				// return;
			// } else {
				// params.matchVal = match;
				// params.resultFun(params.matchUrl, params.matchVal, params.maxResult, plugin.format);
			// }
		});
		params.contentDom.delegate("a", "click", function(e) {
			e.preventDefault();
			if($.isFunction(params.itemClickFun)) {
				params.itemClickFun($(this));
				return;
			} 
			if(params.isSingle) {
				var preVal = target.val();
				var dom = $(this);
				target.val(dom.html());
				target.data("value", dom.attr("id").substr(3));
				if($.isFunction(params.changeFun) && preVal!=target.val()) {
					params.changeFun(preVal, target.val());
				}
				target.keyup();
				plugin.hide();
			} else {
				if($(this).hasClass("mp_on")) {
					$(this).removeClass("mp_on");
				} else {
					$(this).addClass("mp_on");
				}
			}
		});
		plugin.format = function(result, isTrue){
			if(!isTrue) {
				params.contentDom.html('<li style="margin-left:0px;">网络繁忙，请稍后再试！</li>');
				return;
			}
			var html = "";
			if(params.hasAll!=null) {
				html+='<li><a id="mp_'+params.hasAll.id+'">'+params.hasAll.name+'</a></li>';
			}
			for(var i=0; i<result.length; i++) {
				var obj = result[i];
				if(obj.isCustom) {
					html+=obj.html;
				} else {
					html+='<li><a id="mp_'+obj.id+'">'+obj.name+'</a></li>';
				}
			}
			if(html=="") {
				html='<li style="margin-left:0px;">暂无数据</li>';
			}
			params.contentDom.html(html);
		};
		plugin.hide = function() {
			plugin.matchpicker.slideUp("fast");
		};
		plugin.show = function() {
			params.offset = target.offset();
			plugin.matchpicker.css({
				left: params.offset.left+"px",
				top: (params.offset.top+target.outerHeight(true)+1)+"px"
			});
			plugin.matchpicker.slideDown("fast", function(){
				if(params.isDynamic) {
					params.matchDom.focus();
				}
			});
		};
		plugin._mousedown = function(e) {
			if (plugin.matchpicker.css('display') == 'block') {
				if ($(e.target).parents().filter('.Ray_MatchPicker').length == 0) {
					plugin.hide();
				}
			}
		};
		$(document).bind('mousedown', plugin._mousedown);
	};
	$.fn.Ray_MatchPicker_Remove = function() {
		// if element has a date picker already attached
		if (undefined !== $(this).data('Ray_MatchPicker')) {
			var plugin = $(this).data('Ray_MatchPicker');
			plugin.datepicker.remove();
			$(document).unbind('mousedown', plugin._mousedown);
			$(this).removeData('Ray_MatchPicker');
		}
	};
})(jQuery);



















